package com.wf.day_0617.test1;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

/**
 * @author mpk
 * @since 2024/6/17 17:00:59
 */
/*
1.countDownLatch练习：
        让3个子线程，3秒后 同时执行线程任务：打印当前时间*/
public class Demo {


    public static void main(String[] args) {


        CountDownLatch latch = new CountDownLatch(3);

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {

                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + "执行完毕，时间：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    latch.countDown();
                }

            }).start();
        }


        try {
            System.out.println("执行子线程之前的时间：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            latch.await();
            System.out.println("main线程执行完毕");

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


    }


}
